
// When the DOM has loaded
$(document).ready(function() {

	Crafty.init(800, 640).canvas.init();
    Crafty.background("black");

	// Create the menu scene where the player chooses the mode
	Crafty.scene("menu",function() {
		Crafty.background("#F52887");

		// Load the onePlayer scene if clicked
		var onePlayerButton = Crafty.e("2D, DOM, Image, Mouse")
		.image("media/onePlayerButton.png")
		.attr({ x: 200, y: 100 })
		.bind('Click', function() {Crafty.scene("onePlayer")});
		
		// Load the twoPlayers scene if clicked
		var twoPlayerButton = Crafty.e("2D, DOM, Image, Mouse")
		.image("media/twoPlayerButton.png")
		.attr({ x: 200, y: 220 })
		.bind('Click', function() {Crafty.scene("twoPlayer")});
	});

	// Choose the menu scene
	Crafty.scene("menu");
/*

	Crafty.sprite(16, "media/wall.png", {
		wall: [0,0]
	});		

*/  
	// ONE PLAYER MODE ===========================================================================
  	Crafty.scene("onePlayer", function() {
		
		var playerSpeed = 3;
		

		
		// This will create entities called player1 and player2
		Crafty.sprite(16,"images/sprite.png", {
			player: [0,0]
		});
		
	
		Crafty.scene("loading", function() {
			Crafty.load(["images/sprite.png"], function() {
			Crafty.scene("main"); // Run the main scene
			});
		});

		Crafty.scene("loading");
		
		// Draw the player
		Crafty.scene("main",function() {
			Crafty.background("#FFF");
			
			generateWorld();
			
			var player = Crafty.e("2D, Canvas, player")
				.attr({x:18, y:18})
				.bind('KeyDown', function(e) {
					if(e.key == Crafty.keys['SHIFT']) {
					  this.playerSpeed = 10;
					} else {
					  this.playerSpeed = 3;
					}
			});

		/* Movement Code */
		player.addComponent("Multiway").multiway(playerSpeed, {
			UP_ARROW: -90,
			DOWN_ARROW: 90,
			RIGHT_ARROW: 0,
			LEFT_ARROW: 180}).enableControl;
		});			  
	  
	});
	
	// TWO PLAYER MODE =====================================================================
	Crafty.scene("twoPlayer", function() {
  
		// This will create entities called player1 and player2
		Crafty.sprite(32,"media/sprites.png", {
		player1: [0,0],
		player2: [0,3],
		security: [1,1],
		wall: [1,6],
		flower: [3,4]
		});
		
		Crafty.sprite(128,"media/pulse.png", {
			pulse1: [0,0],
			pulse2: [0,1],
			pulse3: [0,2],
			pulse4: [0,3]
		});
		
		Crafty.scene("loading", function() {
			Crafty.load(["media/sprites.png, media/pulse.png"], function() {
			Crafty.scene("main"); // Run the main scene
			});
		});

		Crafty.scene("loading");
		
		// Draw the two players
		Crafty.scene("main",function() {
			Crafty.background("#FFF");
			generateWorld();
			
			var circle1 = Crafty.e("2D, Canvas, SpriteAnimation, pulse1")
			.attr({x:16, y:16, z: 2, aplha: 0.8 })
			.animate('Pulsing', [[0,0],[1,0],[0,0],[0,0]])
			.animate('Pulsing', 30, -1);
			
			var player1 = Crafty.e("2D, Canvas, player1, Player, Slide")
			.attr({x:64, y:64, z: 3});
			var player2 = Crafty.e("2D, Canvas, player2, Player, Slide")
			.attr({x:90, y:90});
			
			/* Movement Code */
			player1.addComponent("Multiway").multiway(2, {
			UP_ARROW: -90,
			DOWN_ARROW: 90,
			RIGHT_ARROW: 0,
			LEFT_ARROW: 180});
			
			circle1.addComponent("Multiway").multiway(2, {
				UP_ARROW: -90,
				DOWN_ARROW: 90,
				RIGHT_ARROW: 0,
				LEFT_ARROW: 180});

			player2.addComponent("Multiway").multiway(2, {
			W: -90,
			S: 90,
			D: 0,
			A: 180});
		});
	});
	});